package com.motong.gongdan.vo.system.dataController.query;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Data
@Accessors(chain = true)
@ApiModel("数据看板工单概览")
public class WorkOrderOverviewQuery {

    @ApiModelProperty(value = "排序字段数组", hidden = true)
    private List<String> orderByList = Arrays.asList("code", "state", "productCode", "productName", "start_time", "end_time", "create_time", "finishProgress");

    @ApiModelProperty(value = "排序排序类型", hidden = true)
    private List<String> sortList = Arrays.asList("ASC", "DESC");

    @ApiModelProperty(value = "状态列表")
    private List<Integer> stateList = Arrays.asList(1, 2);

    @ApiModelProperty("实际开始时间")
    private Date actualStartTime;

    @ApiModelProperty("实际结束时间'")
    private Date actualEndTime;

    @ApiModelProperty("实际开始时间左")
    private Date actualStartTimeLeft;

    @ApiModelProperty("实际开始时间右")
    private Date actualStartTimeRight;

    @ApiModelProperty("实际结束时间左")
    private Date actualEndTimeLeft;

    @ApiModelProperty("实际结束时间右")
    private Date actualEndTimeRight;

    @ApiModelProperty("创建时间左")
    private Date createTimeLeft;

    @ApiModelProperty("创建时间右")
    private Date createTimeRight;

    @ApiModelProperty("排序前缀")
    private String orderByPrefix = "a.";

    @ApiModelProperty("排序字段 （code 工单编号, state 工单状态, productCode 产品编号, productName 产品名称）")
    private String orderBy = orderByPrefix.concat("start_time");

    @ApiModelProperty("排序类型 （ASC 升序, DESC 降序）")
    private String sort = "DESC";

    @ApiModelProperty("页码")
    private Integer page = 1;

    @ApiModelProperty("条数")
    private Integer limit = -1;

    private void setOrderByList(List<String> orderByList) {
    }

    private void setSortList(List<String> sortList) {
    }

    public void setOrderBy(String orderBy) {
        orderBy = (StringUtils.hasLength(orderBy) ? orderBy : "");
        if (orderByList.contains(orderBy)) {
            if (orderBy.startsWith("product")) {
                this.orderByPrefix = "b.";
                orderBy = orderBy.replace("product", "").toLowerCase();
            } else if ("finishProgress".equals(orderBy)) {
                this.orderByPrefix = "";
                orderBy = "allProcessRate";
            }
            this.orderBy = orderByPrefix.concat(orderBy);
            this.orderByPrefix = "a.";
        }
    }

    public void setSort(String sort) {
        sort = (StringUtils.hasLength(sort) ? sort.toUpperCase() : "");
        if (sortList.contains(sort)) {
            this.sort = sort;
        }
    }
}
